<?php
session_start();
if (empty($_SESSION['admin_email'])) {
    echo '尚未登录，请先登录 <a href="index.php?c=login">登录</a>';
    exit();
}
// 获取到文章数据
$articleTitle = $_POST['article_title'];
$categoryId = $_POST['category_id'];
$intro = $_POST['intro'];
$content = $_POST['content'];

if (mb_strlen($articleTitle) < 5 || mb_strlen($articleTitle) > 50) {
    echo '文章标题限制5~50个字';
    echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
    exit();
}
// 10~100
if (mb_strlen($intro) < 10 || mb_strlen($intro) > 100) {
    echo '简介限制10~100个字';
    echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
    exit();
}
// 10~8000
if (mb_strlen($content) < 10 || mb_strlen($content) > 8000) {
    echo '文章内容10~8000个字。';
    echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
    exit();
}
if (empty($categoryId)) {
    echo '需要选择文章分类。';
    echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
    exit();
}

include_once APP_PATH . "./model/article_add_save.php";

if ($result) {
    $log = [
        'action' => 'article_add',
        'content' => '增加文件成功',
        'time' => date("Y-m-d H:i:s", time()),
        'ip' => $_SERVER['REMOTE_ADDR'],
        'article_title' => $articleTitle,
        'category_id' => $categoryId,
        'intro' => $intro,
//        'content' => $content,
    ];
    $file = fopen("./logs/" . date("Y-m-d") . ".txt", 'a+');
    if ($file) {
        fwrite($file, json_encode($log, JSON_UNESCAPED_UNICODE) . PHP_EOL);
        fclose($file);
    } else {
        echo "打开文件错误";
        exit();
    }

    echo "插入成功。<a href='index.php?c=article_list'>返回列表页</a>";
    exit();
} else {
    echo "插入失败，错误信息：" . $db->errorInfo()[2] . "，请联系管理员：303410541@qq.com";
}
